package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TStudentInfoMapper;
import com.ruoyi.system.domain.TStudentInfo;
import com.ruoyi.system.service.ITStudentInfoService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.Md5Utils;

/**
 * 学员信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-28
 */
@Service
public class TStudentInfoServiceImpl implements ITStudentInfoService 
{
    @Autowired
    private TStudentInfoMapper tStudentInfoMapper;

    /**
     * 查询学员信息
     * 
     * @param ID 学员信息ID
     * @return 学员信息
     */
    @Override
    public TStudentInfo selectTStudentInfoById(Long ID)
    {
        return tStudentInfoMapper.selectTStudentInfoById(ID);
    }

    /**
     * 查询学员信息列表
     * 
     * @param tStudentInfo 学员信息
     * @return 学员信息
     */
    @Override
    public List<TStudentInfo> selectTStudentInfoList(TStudentInfo tStudentInfo)
    {
        return tStudentInfoMapper.selectTStudentInfoList(tStudentInfo);
    }

    /**
     * 新增学员信息
     * 
     * @param tStudentInfo 学员信息
     * @return 结果
     */
    @Override
    public int insertTStudentInfo(TStudentInfo tStudentInfo)
    {
    	tStudentInfo.setAddUser(ShiroUtils.getSysUser().getUserId()+"");
        return tStudentInfoMapper.insertTStudentInfo(tStudentInfo);
    }

    /**
     * 修改学员信息
     * 
     * @param tStudentInfo 学员信息
     * @return 结果
     */
    @Override
    public int updateTStudentInfo(TStudentInfo tStudentInfo)
    {
    	tStudentInfo.setUpdateUser(ShiroUtils.getSysUser().getUserId()+"");
        return tStudentInfoMapper.updateTStudentInfo(tStudentInfo);
    }

    /**
     * 删除学员信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTStudentInfoByIds(String ids)
    {
        return tStudentInfoMapper.deleteTStudentInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学员信息信息
     * 
     * @param ID 学员信息ID
     * @return 结果
     */
    @Override
    public int deleteTStudentInfoById(Long ID)
    {
        return tStudentInfoMapper.deleteTStudentInfoById(ID);
    }

	@Override
	public String importStudents(List<TStudentInfo> students) {
		 if (StringUtils.isNull(students) || students.size() == 0)
	        {
	            throw new BusinessException("导入学员数据不能为空！");
	        }
	        int successNum = 0;
	        int failureNum = 0;
	        StringBuilder successMsg = new StringBuilder();
	        StringBuilder failureMsg = new StringBuilder();
	        for (TStudentInfo student : students)
	        {
	            try
	            {
	                // 验证是否存在这个用户
	            	TStudentInfo u = tStudentInfoMapper.selectTStudentInfoByWx(student.getWxNo());
	                if (StringUtils.isNull(u)||!u.getKemu().equals(student.getKemu()))
	                {
	                   
	                    this.insertTStudentInfo(student);
	                    successNum++;
	                    successMsg.append("<br/>" + successNum + "、账号 " + student.getWxNo() + " 导入成功");
	                }else {
	                	 failureNum++;
	 	                String msg = "<br/>" + failureNum + "、账号 " + student.getWxNo() + " 导入失败：";
	 	                failureMsg.append(msg +"已在此科目报名！");
	                }
	             
	            }
	            catch (Exception e)
	            {
	                failureNum++;
	                String msg = "<br/>" + failureNum + "、账号 " + student.getWxNo() + " 导入失败：";
	                failureMsg.append(msg +"已在此科目报名或者微信中有特殊字符无法存入！");

	            }
	        }
	        if (failureNum > 0)
	        {
	            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
	            throw new BusinessException(failureMsg.toString());
	        }
	        else
	        {
	            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
	        }
	        return successMsg.toString();
	}

	@Override
	public List<Map<String,Object>> selectKemuReport() {
		// TODO Auto-generated method stub
		return tStudentInfoMapper.selectKemuReport();
	}

	@Override
	public List<Map<String,Object>> selectYearReport() {
		// TODO Auto-generated method stub
		return tStudentInfoMapper.selectYearReport();
	}
	 public List<Map<String,Object>> selectYearMoneyReport()
	 {
		 return tStudentInfoMapper.selectYearMoneyReport();
	 }
	@Override
	public List<Map<String,Object>> selectMoshiReport() {
		// TODO Auto-generated method stub
		return tStudentInfoMapper.selectMoshiReport();
	}
	
	@Override
	public List<Map<String,Object>> selectKemuHuanbi() {
		// TODO Auto-generated method stub
		return tStudentInfoMapper.selectKemuHuanbi();
	}
}
